package com.wo.cloud.gateway.entity;

import com.wo.cloud.gateway.util.WoCloudConstant;
import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.Getter;

import java.io.Serializable;


/**
 * @author liry
 * @version 1.0
 * @date Created on 2021/3/25 18:10
 * Description: 定义响应结构
 */


@Data
@Getter
@ApiModel(value = "统一响应消息报文")
public class Response implements Serializable {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "状态码", required = true)
    private int code;

    @ApiModelProperty(value = "消息内容", required = true)
    private String message;

    @ApiModelProperty(value = "业务数据")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private Object data;


    private Response(IResultCode resultCode) {
        this(resultCode, null, resultCode.getMsg());
    }

    private Response(IResultCode resultCode, String msg) {
        this(resultCode, null, msg);
    }

    private Response(IResultCode resultCode, Object data) {
        this(resultCode, data, resultCode.getMsg());
    }

    private Response(IResultCode resultCode, Object data, String msg) {
        this(resultCode.getCode(), data, msg);
    }

    private Response(int code, Object data, String msg) {
        this.code = code;
        this.data = data;
        this.message = msg;
    }

    /**
     * 返回状态码
     *
     * @param resultCode 状态码
     * @return ApiResult
     */
    public static Response success(IResultCode resultCode) {
        return new Response(resultCode);
    }

    public static Response success(String msg) {
        return new Response(ResultCode.SUCCESS, msg);
    }

    public static Response success(IResultCode resultCode, String msg) {
        return new Response(resultCode, msg);
    }

    public static Response data(Object data) {
        return data(data, WoCloudConstant.DEFAULT_SUCCESS_MESSAGE);
    }

    public static Response data(Object data, String msg) {
        return data(ResultCode.SUCCESS.code, data, msg);
    }

    public static Response data(int code, Object data, String msg) {
        return new Response(code, data, data == null ? WoCloudConstant.DEFAULT_NULL_MESSAGE : msg);
    }

    public static Response fail() {
        return new Response(ResultCode.FAILURE, ResultCode.FAILURE.getMsg());
    }

    public static Response fail(String msg) {
        return new Response(ResultCode.FAILURE, msg);
    }

    public static Response fail(int code, String msg) {
        return new Response(code, null, msg);
    }

    public static Response fail(IResultCode resultCode) {
        return new Response(resultCode);
    }

    public static Response fail(IResultCode resultCode, String msg) {
        return new Response(resultCode, msg);
    }

    public static Response condition(boolean flag) {
        return flag ? success(WoCloudConstant.DEFAULT_SUCCESS_MESSAGE) : fail(WoCloudConstant.DEFAULT_FAIL_MESSAGE);
    }
}
